<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="ElasticSearch," />










<meta name="description" content="ElasticSearch(了解)一 、基本概念​	全文搜索属于最常见的需求，开源的 Elasticsearch 是目前全文搜索引擎的首选。它可以快速地储存、搜索和分析海量数据。   Index索引 动词：相当于Mysql中的insert 名词：相当于Mysql中的Database  type类型 相当于Mysql中的Table，每一种类型的数据放在一起  Document文档 保存在某个索引下，">
<meta property="og:type" content="article">
<meta property="og:title" content="ElasticSearch">
<meta property="og:url" content="https://gitee.com/htt-sub/htt-sub/2023/10/20/elasticSearch6/index.html">
<meta property="og:site_name" content="黄宏涛">
<meta property="og:description" content="ElasticSearch(了解)一 、基本概念​	全文搜索属于最常见的需求，开源的 Elasticsearch 是目前全文搜索引擎的首选。它可以快速地储存、搜索和分析海量数据。   Index索引 动词：相当于Mysql中的insert 名词：相当于Mysql中的Database  type类型 相当于Mysql中的Table，每一种类型的数据放在一起  Document文档 保存在某个索引下，">
<meta property="og:locale">
<meta property="og:image" content="https://gitee.com/htt-sub/pic-go/raw/master/images/image-20231106095237546.png">
<meta property="og:image" content="c:\Users\ZC\AppData\Roaming\Typora\typora-user-images\image-20231022170007666.png">
<meta property="article:published_time" content="2023-10-19T16:00:00.000Z">
<meta property="article:modified_time" content="2023-11-06T11:30:31.916Z">
<meta property="article:author" content="黄宏涛">
<meta property="article:tag" content="ElasticSearch">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/htt-sub/pic-go/raw/master/images/image-20231106095237546.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"post","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://gitee.com/htt-sub/htt-sub/2023/10/20/elasticSearch6/"/>





  <title>ElasticSearch | 黄宏涛</title>
  








<meta name="generator" content="Hexo 6.3.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-post page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">黄宏涛</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://gitee.com/htt-sub/htt-sub/2023/10/20/elasticSearch6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="黄宏涛">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">ElasticSearch</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2023-10-20T00:00:00+08:00">
                2023-10-20
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/ElasticSearch/" itemprop="url" rel="index">
                    <span itemprop="name">ElasticSearch</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="ElasticSearch-了解"><a href="#ElasticSearch-了解" class="headerlink" title="ElasticSearch(了解)"></a>ElasticSearch(了解)</h1><h2 id="一-、基本概念"><a href="#一-、基本概念" class="headerlink" title="一 、基本概念"></a>一 、基本概念</h2><p>​	全文搜索属于最常见的需求，开源的 Elasticsearch 是目前<strong>全文搜索引擎的首选</strong>。<br>它可以<strong>快速地储存、搜索和分析海量数据</strong>。</p>
<p><img src="https://gitee.com/htt-sub/pic-go/raw/master/images/image-20231106095237546.png" alt="image-20231106095237546"></p>
<ol>
<li><p>Index索引</p>
<p>动词：相当于Mysql中的insert</p>
<p>名词：相当于Mysql中的<strong>Database</strong></p>
</li>
<li><p>type类型</p>
<p>相当于Mysql中的<strong>Table</strong>，每一种类型的数据放在一起</p>
</li>
<li><p>Document文档</p>
<p>保存在某个索引下，某种类型的一个数据（document），文档是JSON格式。<strong>Document</strong>就是Mysql中某个<strong>Table里面的内容</strong></p>
</li>
<li><p>倒排索引机制</p>
<p><img src="C:\Users\ZC\AppData\Roaming\Typora\typora-user-images\image-20231022170007666.png" alt="image-20231022170007666"></p>
</li>
</ol>
<p>​	分词：将整句拆为单词</p>
<p>​	保存的记录：</p>
<ol>
<li>红海行动                     </li>
<li>探索红海行动</li>
<li>红海特别行动</li>
<li>红海记录篇</li>
<li>特工红海特别探索</li>
</ol>
<p>​	检索：</p>
<ol>
<li><p>红海特工行动</p>
</li>
<li><p>红海行动</p>
<p><strong>根据搜索出来的结果进行相关性评分</strong></p>
</li>
</ol>
<h2 id="二-、Docker安装"><a href="#二-、Docker安装" class="headerlink" title="二 、Docker安装"></a>二 、Docker安装</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">docker pull elasticsearch:<span class="number">7.4</span><span class="number">.2</span> 存储和检索数据</span><br><span class="line">    </span><br><span class="line">docker pull kibana:<span class="number">7.4</span><span class="number">.2</span> 可视化检索数据</span><br><span class="line">    </span><br><span class="line">mkdir -p /mydata/elasticsearch/config</span><br><span class="line">    </span><br><span class="line">mkdir -p /mydata/elasticsearch/data</span><br><span class="line">    </span><br><span class="line">echo <span class="string">&quot;http.host: 0.0.0.0&quot;</span> &gt;&gt; /mydata/elasticsearch/config/elasticsearch.yml</span><br><span class="line">    </span><br><span class="line">chmod -R <span class="number">777</span> /mydata/elasticsearch/ 保证权限</span><br><span class="line">    </span><br><span class="line">docker run --name elasticsearch -p <span class="number">9200</span>:<span class="number">9200</span> -p <span class="number">9300</span>:<span class="number">9300</span> -e <span class="string">&quot;discovery.type=single-node&quot;</span> -e ES_JAVA_OPTS=<span class="string">&quot;-Xms64m -Xmx512m&quot;</span> -v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /mydata/elasticsearch/data:/usr/share/elasticsearch/data -v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins -d elasticsearch:<span class="number">7.4</span><span class="number">.2</span></span><br></pre></td></tr></table></figure>

<h3 id="Kibanna可视化界面"><a href="#Kibanna可视化界面" class="headerlink" title="Kibanna可视化界面"></a>Kibanna可视化界面</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">docker run --name kibana -e ELASTICSEARCH_HOSTS=http:<span class="comment">//192.168.253.128:9200 -p 5601:5601 -d kibana:7.4.2</span></span><br><span class="line">    </span><br><span class="line">http:<span class="comment">//192.168.56.10:9200 一定改为自己虚拟机的地址</span></span><br></pre></td></tr></table></figure>

<h2 id="三、初步探索"><a href="#三、初步探索" class="headerlink" title="三、初步探索"></a>三、初步探索</h2><h3 id="1-cat"><a href="#1-cat" class="headerlink" title="1. _cat"></a>1. _cat</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">GET /_cat/nodes:查看所有节点</span><br><span class="line">GET /_cat/health:查看es健康状况</span><br><span class="line">GET/_cat/master:查看主节点</span><br><span class="line">GET/_cat/indices:查看所有索引</span><br><span class="line">show databases;</span><br></pre></td></tr></table></figure>

<h3 id="2-索引一个文档（保存和修改）"><a href="#2-索引一个文档（保存和修改）" class="headerlink" title="2. 索引一个文档（保存和修改）"></a>2. 索引一个文档（<strong>保存和修改</strong>）</h3><p>​	保存一个数据，保存在哪个索引的哪个类型下（<strong>保存到数据库的哪个表下</strong>），指定用哪个唯一标识</p>
<p>​	例如：在customer索引下的external类型下保存1号数据(<strong>JSON格式</strong>)</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">PUT customer<span class="operator">/</span><span class="keyword">external</span><span class="operator">/</span><span class="number">1</span></span><br></pre></td></tr></table></figure>

<p>​	1号数据为</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#123;&quot;name&quot;:&quot;黄宏涛&quot;&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">http:<span class="operator">/</span><span class="operator">/</span><span class="number">192.168</span><span class="number">.253</span><span class="number">.128</span>:<span class="number">9200</span><span class="operator">/</span>customer<span class="operator">/</span><span class="keyword">external</span><span class="operator">/</span><span class="number">1</span></span><br><span class="line">参数为&#123;&quot;name&quot;:&quot;黄宏涛&quot;&#125;</span><br><span class="line"></span><br><span class="line">发送多次是一个更新操作</span><br><span class="line"></span><br><span class="line">响应为</span><br><span class="line">&#123;</span><br><span class="line">    &quot;_index&quot;: &quot;customer&quot;,   数据库名</span><br><span class="line">    &quot;_type&quot;: &quot;external&quot;,    表</span><br><span class="line">    &quot;_id&quot;: &quot;1&quot;,</span><br><span class="line">    &quot;_version&quot;: <span class="number">1</span>,</span><br><span class="line">    &quot;result&quot;: &quot;created&quot;,    结果：新建</span><br><span class="line">    &quot;_shards&quot;: &#123;            集群：分片</span><br><span class="line">        &quot;total&quot;: <span class="number">2</span>,</span><br><span class="line">        &quot;successful&quot;: <span class="number">1</span>,</span><br><span class="line">        &quot;failed&quot;: <span class="number">0</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;_seq_no&quot;: <span class="number">0</span>,</span><br><span class="line">    &quot;_primary_term&quot;: <span class="number">1</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="3-POST和PUT区别"><a href="#3-POST和PUT区别" class="headerlink" title="3. POST和PUT区别"></a>3. POST和PUT区别</h3><ol>
<li>PUT必须指定id</li>
<li>POST可以不指定id，会自动生成id，指定id就会修改这个数据</li>
<li><strong>POST一般用来新增，PUT一般用来修改</strong></li>
<li>POST和PUT都可以用来新增和修改</li>
</ol>
<h3 id="3-查询文档（query）"><a href="#3-查询文档（query）" class="headerlink" title="3. 查询文档（query）"></a>3. 查询文档（query）</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">GET</span> customer<span class="operator">/</span><span class="keyword">external</span><span class="operator">/</span><span class="number">1</span></span><br></pre></td></tr></table></figure>

<p>响应：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    &quot;_index&quot;: &quot;customer&quot;,</span><br><span class="line">    &quot;_type&quot;: &quot;external&quot;,</span><br><span class="line">    &quot;_id&quot;: &quot;1&quot;,</span><br><span class="line">    &quot;_version&quot;: <span class="number">1</span>,        版本号</span><br><span class="line">    &quot;_seq_no&quot;: <span class="number">0</span>,         并发控制模块，每次更新就会加一，用来做乐观锁</span><br><span class="line">    &quot;_primary_term&quot;: <span class="number">1</span>,   同上、主分片重新分配，如重启，就会变化</span><br><span class="line">    &quot;found&quot;: <span class="literal">true</span>,        代表找到了这个数据</span><br><span class="line">    &quot;_source&quot;: &#123;          真正的内容</span><br><span class="line">        &quot;name&quot;: &quot;黄宏涛&quot;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">乐观锁</span><br><span class="line">A和B同时并发访问一个数据，只有&quot;_seq_no&quot;和&quot;_primary_term&quot;符合要求才会更新</span><br><span class="line"></span><br><span class="line">http:<span class="operator">/</span><span class="operator">/</span><span class="number">192.168</span><span class="number">.253</span><span class="number">.128</span>:<span class="number">9200</span><span class="operator">/</span>customer<span class="operator">/</span><span class="keyword">external</span><span class="operator">/</span><span class="number">1</span>?if_seq_no<span class="operator">=</span><span class="number">0</span><span class="operator">&amp;</span>if_primary_term<span class="operator">=</span><span class="number">1</span></span><br></pre></td></tr></table></figure>

<h3 id="4-更新文档（update）"><a href="#4-更新文档（update）" class="headerlink" title="4. 更新文档（update）"></a>4. 更新文档（update）</h3><h4 id="1-POST更新—-update——必须带-“doc”"><a href="#1-POST更新—-update——必须带-“doc”" class="headerlink" title="1. POST更新—-_update——必须带 “doc”"></a>1. POST更新—-_update——必须带 “doc”</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">http:<span class="operator">/</span><span class="operator">/</span><span class="number">192.168</span><span class="number">.253</span><span class="number">.128</span>:<span class="number">9200</span><span class="operator">/</span>customer<span class="operator">/</span><span class="keyword">external</span><span class="operator">/</span><span class="number">1</span><span class="operator">/</span>_update</span><br><span class="line">参数：</span><br><span class="line">&#123;</span><br><span class="line">    &quot;doc&quot;:&#123;</span><br><span class="line">        &quot;name&quot;:&quot;黄宏涛&quot;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">相同参数的多次更新，会对比原来的数据，如果一样，版本号等不会变化</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="2-POST更新—-不带-update—-不带“doc”"><a href="#2-POST更新—-不带-update—-不带“doc”" class="headerlink" title="2. POST更新—-不带_update—-不带“doc”"></a>2. POST更新—-不带_update—-不带“doc”</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">http:<span class="operator">/</span><span class="operator">/</span><span class="number">192.168</span><span class="number">.253</span><span class="number">.128</span>:<span class="number">9200</span><span class="operator">/</span>customer<span class="operator">/</span><span class="keyword">external</span><span class="operator">/</span><span class="number">1</span></span><br><span class="line">参数：</span><br><span class="line">&#123;</span><br><span class="line">        &quot;name&quot;:&quot;黄宏涛&quot;</span><br><span class="line">&#125;</span><br><span class="line">不会与原数据比较，直接更新，版本号等 也会更新</span><br></pre></td></tr></table></figure>

<h4 id="3-PUT更新和2相同"><a href="#3-PUT更新和2相同" class="headerlink" title="3. PUT更新和2相同"></a>3. PUT更新和2相同</h4><h4 id="4-POST—带-update更新同时增加属性"><a href="#4-POST—带-update更新同时增加属性" class="headerlink" title="4. POST—带_update更新同时增加属性"></a>4. POST—带_update更新同时增加属性</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">   &quot;doc&quot;:&#123;</span><br><span class="line">        &quot;name&quot;:&quot;黄宏涛&quot;,</span><br><span class="line">        &quot;age&quot;:<span class="number">20</span></span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="5-PUT-更新同时增加属性"><a href="#5-PUT-更新同时增加属性" class="headerlink" title="5. PUT 更新同时增加属性"></a>5. PUT 更新同时增加属性</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">        &quot;name&quot;:&quot;黄宏涛&quot;,</span><br><span class="line">        &quot;age&quot;:<span class="number">20</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="5-删除文档-索引"><a href="#5-删除文档-索引" class="headerlink" title="5. 删除文档&amp;索引"></a>5. 删除文档&amp;索引</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">DELETE</span> http:<span class="operator">/</span><span class="operator">/</span><span class="number">192.168</span><span class="number">.253</span><span class="number">.128</span>:<span class="number">9200</span><span class="operator">/</span>customer<span class="operator">/</span><span class="keyword">external</span><span class="operator">/</span><span class="number">1</span></span><br><span class="line"><span class="keyword">DELETE</span> customer 删除索引</span><br><span class="line">没有删除类型的功能</span><br></pre></td></tr></table></figure>

<h3 id="6-bulk批量API"><a href="#6-bulk批量API" class="headerlink" title="6. bulk批量API"></a>6. bulk批量API</h3><p><strong><span style="color:red">每两行算作一个数据</span></strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line">POST http:<span class="operator">/</span><span class="operator">/</span><span class="number">192.168</span><span class="number">.253</span><span class="number">.128</span>:<span class="number">9200</span><span class="operator">/</span>customer<span class="operator">/</span><span class="keyword">external</span><span class="operator">/</span>_bulk</span><br><span class="line"></span><br><span class="line">参数：</span><br><span class="line">&#123;&quot;index&quot;:&#123;&quot;_id&quot;:&quot;1&quot;&#125;&#125;</span><br><span class="line">&#123;&quot;name&quot;:&quot;黄宏涛&quot;&#125;</span><br><span class="line"></span><br><span class="line">&#123;&quot;index&quot;:&#123;&quot;_id&quot;:&quot;2&quot;&#125;&#125;</span><br><span class="line">&#123;&quot;name&quot;:&quot;211&quot;&#125;</span><br><span class="line">注意：参数这里需要有一个换行，否则postman报错</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">响应：</span><br><span class="line">&#123;</span><br><span class="line">    &quot;took&quot;: <span class="number">7</span>,</span><br><span class="line">    &quot;errors&quot;: <span class="literal">false</span>,</span><br><span class="line">    &quot;items&quot;: [</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;index&quot;: &#123;</span><br><span class="line">                &quot;_index&quot;: &quot;customer&quot;,</span><br><span class="line">                &quot;_type&quot;: &quot;external&quot;,</span><br><span class="line">                &quot;_id&quot;: &quot;1&quot;,</span><br><span class="line">                &quot;_version&quot;: <span class="number">1</span>,</span><br><span class="line">                &quot;result&quot;: &quot;created&quot;,</span><br><span class="line">                &quot;_shards&quot;: &#123;</span><br><span class="line">                    &quot;total&quot;: <span class="number">2</span>,</span><br><span class="line">                    &quot;successful&quot;: <span class="number">1</span>,</span><br><span class="line">                    &quot;failed&quot;: <span class="number">0</span></span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;_seq_no&quot;: <span class="number">9</span>,</span><br><span class="line">                &quot;_primary_term&quot;: <span class="number">1</span>,</span><br><span class="line">                &quot;status&quot;: <span class="number">201</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;index&quot;: &#123;</span><br><span class="line">                &quot;_index&quot;: &quot;customer&quot;,</span><br><span class="line">                &quot;_type&quot;: &quot;external&quot;,</span><br><span class="line">                &quot;_id&quot;: &quot;2&quot;,</span><br><span class="line">                &quot;_version&quot;: <span class="number">1</span>,</span><br><span class="line">                &quot;result&quot;: &quot;created&quot;,</span><br><span class="line">                &quot;_shards&quot;: &#123;</span><br><span class="line">                    &quot;total&quot;: <span class="number">2</span>,</span><br><span class="line">                    &quot;successful&quot;: <span class="number">1</span>,</span><br><span class="line">                    &quot;failed&quot;: <span class="number">0</span></span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;_seq_no&quot;: <span class="number">10</span>,</span><br><span class="line">                &quot;_primary_term&quot;: <span class="number">1</span>,</span><br><span class="line">                &quot;status&quot;: <span class="number">201</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>​	<strong>复杂实例：</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br></pre></td><td class="code"><pre><span class="line">POST <span class="operator">/</span>_bulk    没有指定索引和类型，则对整个ElasticSearch进行批量操作</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#123; &quot;delete&quot;: &#123; &quot;_index&quot;: &quot;website&quot;, &quot;_type&quot;: &quot;blog&quot;, &quot;_id&quot;: &quot;123&quot; &#125;&#125;</span><br><span class="line"></span><br><span class="line">&#123; &quot;create&quot;: &#123; &quot;_index&quot;: &quot;website&quot;, &quot;_type&quot;: &quot;blog&quot;, &quot;_id&quot;: &quot;123&quot; &#125;&#125;</span><br><span class="line">&#123; &quot;title&quot;: &quot;My first blog post&quot; &#125;</span><br><span class="line"></span><br><span class="line">&#123; &quot;index&quot;: &#123; &quot;_index&quot;: &quot;website&quot;, &quot;_type&quot;: &quot;blog&quot; &#125;&#125;</span><br><span class="line">&#123; &quot;title&quot;: &quot;My second blog post&quot; &#125;</span><br><span class="line"></span><br><span class="line">&#123; &quot;update&quot;: &#123; &quot;_index&quot;: &quot;website&quot;, &quot;_type&quot;: &quot;blog&quot;, &quot;_id&quot;: &quot;123&quot;&#125; &#125;</span><br><span class="line">&#123; &quot;doc&quot; : &#123;&quot;title&quot; : &quot;My updated blog post&quot;&#125; &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">响应：</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;took&quot;: <span class="number">139</span>,</span><br><span class="line">    &quot;errors&quot;: <span class="literal">false</span>,</span><br><span class="line">    &quot;items&quot;: [</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;delete&quot;: &#123;</span><br><span class="line">                &quot;_index&quot;: &quot;website&quot;,</span><br><span class="line">                &quot;_type&quot;: &quot;blog&quot;,</span><br><span class="line">                &quot;_id&quot;: &quot;123&quot;,</span><br><span class="line">                &quot;_version&quot;: <span class="number">1</span>,</span><br><span class="line">                &quot;result&quot;: &quot;not_found&quot;,</span><br><span class="line">                &quot;_shards&quot;: &#123;</span><br><span class="line">                    &quot;total&quot;: <span class="number">2</span>,</span><br><span class="line">                    &quot;successful&quot;: <span class="number">1</span>,</span><br><span class="line">                    &quot;failed&quot;: <span class="number">0</span></span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;_seq_no&quot;: <span class="number">0</span>,</span><br><span class="line">                &quot;_primary_term&quot;: <span class="number">1</span>,</span><br><span class="line">                &quot;status&quot;: <span class="number">404</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;create&quot;: &#123;</span><br><span class="line">                &quot;_index&quot;: &quot;website&quot;,</span><br><span class="line">                &quot;_type&quot;: &quot;blog&quot;,</span><br><span class="line">                &quot;_id&quot;: &quot;123&quot;,</span><br><span class="line">                &quot;_version&quot;: <span class="number">2</span>,</span><br><span class="line">                &quot;result&quot;: &quot;created&quot;,</span><br><span class="line">                &quot;_shards&quot;: &#123;</span><br><span class="line">                    &quot;total&quot;: <span class="number">2</span>,</span><br><span class="line">                    &quot;successful&quot;: <span class="number">1</span>,</span><br><span class="line">                    &quot;failed&quot;: <span class="number">0</span></span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;_seq_no&quot;: <span class="number">1</span>,</span><br><span class="line">                &quot;_primary_term&quot;: <span class="number">1</span>,</span><br><span class="line">                &quot;status&quot;: <span class="number">201</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;index&quot;: &#123;</span><br><span class="line">                &quot;_index&quot;: &quot;website&quot;,</span><br><span class="line">                &quot;_type&quot;: &quot;blog&quot;,</span><br><span class="line">                &quot;_id&quot;: &quot;H-QfV4sBbPM0MaokNgHh&quot;,</span><br><span class="line">                &quot;_version&quot;: <span class="number">1</span>,</span><br><span class="line">                &quot;result&quot;: &quot;created&quot;,</span><br><span class="line">                &quot;_shards&quot;: &#123;</span><br><span class="line">                    &quot;total&quot;: <span class="number">2</span>,</span><br><span class="line">                    &quot;successful&quot;: <span class="number">1</span>,</span><br><span class="line">                    &quot;failed&quot;: <span class="number">0</span></span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;_seq_no&quot;: <span class="number">2</span>,</span><br><span class="line">                &quot;_primary_term&quot;: <span class="number">1</span>,</span><br><span class="line">                &quot;status&quot;: <span class="number">201</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;update&quot;: &#123;</span><br><span class="line">                &quot;_index&quot;: &quot;website&quot;,</span><br><span class="line">                &quot;_type&quot;: &quot;blog&quot;,</span><br><span class="line">                &quot;_id&quot;: &quot;123&quot;,</span><br><span class="line">                &quot;_version&quot;: <span class="number">3</span>,</span><br><span class="line">                &quot;result&quot;: &quot;updated&quot;,</span><br><span class="line">                &quot;_shards&quot;: &#123;</span><br><span class="line">                    &quot;total&quot;: <span class="number">2</span>,</span><br><span class="line">                    &quot;successful&quot;: <span class="number">1</span>,</span><br><span class="line">                    &quot;failed&quot;: <span class="number">0</span></span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;_seq_no&quot;: <span class="number">3</span>,</span><br><span class="line">                &quot;_primary_term&quot;: <span class="number">1</span>,</span><br><span class="line">                &quot;status&quot;: <span class="number">200</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="四、进阶检索"><a href="#四、进阶检索" class="headerlink" title="四、进阶检索"></a>四、进阶检索</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">测试数据</span><br><span class="line">https:<span class="operator">/</span><span class="operator">/</span>gitee.com<span class="operator">/</span>xlh_blog<span class="operator">/</span>common_content<span class="operator">/</span><span class="type">blob</span><span class="operator">/</span>master<span class="operator">/</span>es<span class="operator">%</span>E6<span class="operator">%</span>B5<span class="operator">%</span><span class="number">8</span>B<span class="operator">%</span>E8<span class="operator">%</span>AF<span class="operator">%</span><span class="number">95</span><span class="operator">%</span>E6<span class="operator">%</span><span class="number">95</span><span class="operator">%</span>B0<span class="operator">%</span>E6<span class="operator">%</span><span class="number">8</span>D<span class="operator">%</span>AE.json#</span><br></pre></td></tr></table></figure>

<h3 id="1-SearchAPI"><a href="#1-SearchAPI" class="headerlink" title="1. SearchAPI"></a>1. SearchAPI</h3><p>ES支持两种方式的检索：</p>
<p>一个使用REST request 发送搜索参数（<strong>url+检索参数</strong>）</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">GET</span> http:<span class="operator">/</span><span class="operator">/</span><span class="number">192.168</span><span class="number">.253</span><span class="number">.128</span>:<span class="number">9200</span><span class="operator">/</span>bank<span class="operator">/</span>_search?q<span class="operator">=</span><span class="operator">*</span><span class="operator">&amp;</span>sort<span class="operator">=</span>account_number:<span class="keyword">asc</span></span><br><span class="line">q<span class="operator">=</span><span class="operator">*</span>代表查询所有。安装account_number升序排列</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;took&quot;: <span class="number">19</span>,</span><br><span class="line">    &quot;timed_out&quot;: <span class="literal">false</span>,</span><br><span class="line">    &quot;_shards&quot;: &#123;</span><br><span class="line">        &quot;total&quot;: <span class="number">1</span>,</span><br><span class="line">        &quot;successful&quot;: <span class="number">1</span>,</span><br><span class="line">        &quot;skipped&quot;: <span class="number">0</span>,</span><br><span class="line">        &quot;failed&quot;: <span class="number">0</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;hits&quot;: &#123;</span><br><span class="line">        &quot;total&quot;: &#123;</span><br><span class="line">            &quot;value&quot;: <span class="number">1000</span>,      总共查询到的记录数</span><br><span class="line">            &quot;relation&quot;: &quot;eq&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;max_score&quot;: <span class="keyword">null</span>,      评估得分，因为查询的是所有，没有评估分</span><br><span class="line">        &quot;hits&quot;:[</span><br><span class="line">            ......</span><br><span class="line">        ]</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>一个使用RESTrequest body 发送（<strong>url+请求体</strong>），<strong>推荐使用</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>:&#123;</span><br><span class="line">        <span class="string">&quot;match_all&quot;</span>:&#123;&#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;sort&quot;</span>:[</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">&quot;account_number&quot;</span>:<span class="string">&quot;asc&quot;</span>   先按照账号升序，账号相同按照余额降序</span><br><span class="line">        &#125;,&#123;</span><br><span class="line">            <span class="string">&quot;balance&quot;</span>:<span class="string">&quot;desc&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    ]，</span><br><span class="line">    <span class="string">&quot;from&quot;</span>:<span class="number">10</span>,</span><br><span class="line">    <span class="string">&quot;size&quot;</span>:<span class="number">10</span>         				获取从<span class="number">10</span>到<span class="number">19</span>之间的结果，从第<span class="number">10</span>条数据开始拿<span class="number">10</span>条</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">查询五条数据</span><br><span class="line">&#123;</span><br><span class="line">    &quot;query&quot;:&#123;</span><br><span class="line">        &quot;match_all&quot;:&#123;&#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;from&quot;:<span class="number">1</span>,</span><br><span class="line">    &quot;size&quot;:<span class="number">5</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">只返回指定字段&quot;_source&quot;</span><br><span class="line">&#123;</span><br><span class="line">    &quot;query&quot;:&#123;</span><br><span class="line">        &quot;match_all&quot;:&#123;&#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;_source&quot;:[&quot;balance&quot;,&quot;firstname&quot;],</span><br><span class="line">    &quot;from&quot;:<span class="number">1</span>,</span><br><span class="line">    &quot;size&quot;:<span class="number">5</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-match匹配查询"><a href="#2-match匹配查询" class="headerlink" title="2. match匹配查询"></a>2. match匹配查询</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">GET</span> http:<span class="operator">/</span><span class="operator">/</span><span class="number">192.168</span><span class="number">.253</span><span class="number">.128</span>:<span class="number">9200</span><span class="operator">/</span>bank<span class="operator">/</span>_search</span><br><span class="line"></span><br><span class="line">精确查询</span><br><span class="line">&#123;</span><br><span class="line">    &quot;query&quot;:&#123;</span><br><span class="line">        &quot;match&quot;:&#123;</span><br><span class="line">            &quot;account_number&quot;:&quot;20&quot;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">模糊查询，当查询条件为多个字符串，会进行分词匹配</span><br><span class="line">&#123;</span><br><span class="line">    &quot;query&quot;:&#123;</span><br><span class="line">        &quot;match&quot;:&#123;</span><br><span class="line">            &quot;address&quot;:&quot;Kings&quot;   查询出所有包含Kings的数据。</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&#123;</span><br><span class="line">                &quot;_index&quot;: &quot;bank&quot;,</span><br><span class="line">                &quot;_type&quot;: &quot;account&quot;,</span><br><span class="line">                &quot;_id&quot;: &quot;20&quot;,</span><br><span class="line">                &quot;_score&quot;: <span class="number">5.9908285</span>,             得分<span class="number">5.99</span> ，倒排索引，地址为三个字符串，中了一个</span><br><span class="line">                &quot;_source&quot;: &#123;</span><br><span class="line">                    &quot;account_number&quot;: <span class="number">20</span>,</span><br><span class="line">                    &quot;balance&quot;: <span class="number">16418</span>,</span><br><span class="line">                    &quot;firstname&quot;: &quot;Elinor&quot;,</span><br><span class="line">                    &quot;lastname&quot;: &quot;Ratliff&quot;,</span><br><span class="line">                    &quot;age&quot;: <span class="number">36</span>,</span><br><span class="line">                    &quot;gender&quot;: &quot;M&quot;,</span><br><span class="line">                    &quot;address&quot;: &quot;282 Kings Place&quot;,</span><br><span class="line">                    &quot;employer&quot;: &quot;Scentric&quot;,</span><br><span class="line">                    &quot;email&quot;: &quot;elinorratliff@scentric.com&quot;,</span><br><span class="line">                    &quot;city&quot;: &quot;Ribera&quot;,</span><br><span class="line">                    &quot;state&quot;: &quot;WA&quot;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br></pre></td></tr></table></figure>

<h3 id="3-match-phrase短语匹配"><a href="#3-match-phrase短语匹配" class="headerlink" title="3. match_phrase短语匹配"></a>3. match_phrase短语匹配</h3><p>“match_phrase”把<strong>匹配的值当成一个整体单词（不分词）检索</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">match_phrase模糊查询，把地址当作一个整体</span><br><span class="line">&#123;</span><br><span class="line">    &quot;query&quot;:&#123;</span><br><span class="line">        &quot;match_phrase&quot;:&#123;</span><br><span class="line">            &quot;address&quot;:&quot;Kings Road&quot;    </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">address.keyword是精确匹配，地址中只能有我们要匹配的值</span><br><span class="line">&#123;</span><br><span class="line">    &quot;query&quot;:&#123;</span><br><span class="line">       &quot;match&quot;:&#123;</span><br><span class="line">           &quot;address.keyword&quot;:&quot;Kings Road&quot;    </span><br><span class="line">       &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="4-multi-match多字段匹配"><a href="#4-multi-match多字段匹配" class="headerlink" title="4. multi_match多字段匹配"></a>4. multi_match多字段匹配</h3><p>“multi_match”多字段匹配</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    &quot;query&quot;:&#123;</span><br><span class="line">       &quot;multi_match&quot;:&#123;</span><br><span class="line">           &quot;query&quot;:&quot;mill&quot;,</span><br><span class="line">           &quot;fields&quot;:[&quot;address&quot;,&quot;city&quot;]    </span><br><span class="line">       &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="5-bool复合查询"><a href="#5-bool复合查询" class="headerlink" title="5. bool复合查询"></a>5. bool复合查询</h3><p>可以合并任何其它查询语句，包含复合语句，<strong>复合语句之间可以嵌套</strong>。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    &quot;query&quot;:&#123;</span><br><span class="line">       &quot;bool&quot;:&#123;</span><br><span class="line">           &quot;must&quot;:[                         必须满足gender为&quot;F&quot;，address为&quot;mill&quot;</span><br><span class="line">               &#123;&quot;match&quot;:&#123;</span><br><span class="line">                    &quot;gender&quot;:&quot;M&quot;</span><br><span class="line">                &#125;&#125;,</span><br><span class="line">               &#123;&quot;match&quot;:&#123;</span><br><span class="line">                   &quot;address&quot;:&quot;mill&quot;</span><br><span class="line">               &#125;&#125;</span><br><span class="line">           ],</span><br><span class="line">           &quot;must_not&quot;:[                    不能包括年龄为<span class="number">28</span>的</span><br><span class="line">               &#123;&quot;match&quot;:&#123;&quot;age&quot;:<span class="number">28</span>&#125;&#125;</span><br><span class="line">           ],</span><br><span class="line">           &quot;should&quot;:[                      &quot;last_name&quot;最后一个名字为Wallace的可以有，也可以没有,但是										满足的话得分会更高</span><br><span class="line">               &#123;&quot;&quot;</span><br><span class="line">                   &quot;match&quot;:&#123;</span><br><span class="line">                       &quot;last_name&quot;:&quot;Wallace&quot;</span><br><span class="line">                   &#125;</span><br><span class="line">               &#125;</span><br><span class="line">           ]</span><br><span class="line">       &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="6-结果过滤filter"><a href="#6-结果过滤filter" class="headerlink" title="6. 结果过滤filter"></a>6. 结果过滤filter</h3><p><strong>filter对得分没有影响，对查询出的所有结果不会评分</strong></p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">采用must查询age在<span class="number">18</span>到<span class="number">30</span>之间的数据</span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;query&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">       <span class="attr">&quot;bool&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">           <span class="attr">&quot;must&quot;</span><span class="punctuation">:</span><span class="punctuation">[</span></span><br><span class="line">               <span class="punctuation">&#123;</span></span><br><span class="line">                   <span class="attr">&quot;range&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">                       <span class="attr">&quot;age&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">                           <span class="attr">&quot;gte&quot;</span><span class="punctuation">:</span><span class="number">18</span><span class="punctuation">,</span></span><br><span class="line">                           <span class="attr">&quot;lte&quot;</span><span class="punctuation">:</span><span class="number">30</span></span><br><span class="line">                       <span class="punctuation">&#125;</span></span><br><span class="line">                   <span class="punctuation">&#125;</span></span><br><span class="line">               <span class="punctuation">&#125;</span></span><br><span class="line">           <span class="punctuation">]</span></span><br><span class="line">       <span class="punctuation">&#125;</span></span><br><span class="line">    <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br><span class="line">采用filter</span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;query&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">       <span class="attr">&quot;bool&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">           <span class="attr">&quot;filter&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">               <span class="attr">&quot;range&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">                   <span class="attr">&quot;age&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">                       <span class="attr">&quot;gte&quot;</span><span class="punctuation">:</span><span class="number">18</span><span class="punctuation">,</span></span><br><span class="line">                       <span class="attr">&quot;lte&quot;</span><span class="punctuation">:</span><span class="number">30</span></span><br><span class="line">                   <span class="punctuation">&#125;</span></span><br><span class="line">               <span class="punctuation">&#125;</span></span><br><span class="line">           <span class="punctuation">&#125;</span></span><br><span class="line">       <span class="punctuation">&#125;</span></span><br><span class="line">    <span class="punctuation">&#125;</span></span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>

<h3 id="7-term"><a href="#7-term" class="headerlink" title="7. term"></a>7. term</h3><p><strong>全文检索字段</strong>(文本字段)使用match匹配</p>
<p>非文本字段用term </p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    &quot;query&quot;:&#123;</span><br><span class="line">        &quot;term&quot;:&#123;</span><br><span class="line">            &quot;balance&quot;:<span class="number">32838</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="8-aggregations执行聚合"><a href="#8-aggregations执行聚合" class="headerlink" title="8. aggregations执行聚合"></a>8. aggregations执行聚合</h3><p>提供了从数据中分组和提取数据的能力，最简单的聚合方法大致等于SQL  GROUP By 和SQL聚合函数</p>
<p>把一个响应中的所有hits（命中结果）分隔开的能力。</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">搜索 address 中包含 mill 的所有人的年龄分布以及平均年龄，但不显示这些人的详情</span><br><span class="line"><span class="punctuation">&#123;</span></span><br><span class="line">    <span class="attr">&quot;query&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">        <span class="attr">&quot;match&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span></span><br><span class="line">            <span class="attr">&quot;address&quot;</span><span class="punctuation">:</span><span class="string">&quot;mill&quot;</span></span><br><span class="line">        <span class="punctuation">&#125;</span></span><br><span class="line">    <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;aggs&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span>                  表示聚合</span><br><span class="line">    <span class="attr">&quot;ageAgg&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span>                自定义的聚合名</span><br><span class="line">        <span class="attr">&quot;terms&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span>             terms表示值有多少种指定字段的值有多少分组，例如：年龄<span class="number">38</span>的<span class="number">2</span>个，<span class="number">28</span>的<span class="number">1</span>个</span><br><span class="line">            <span class="attr">&quot;field&quot;</span><span class="punctuation">:</span><span class="string">&quot;age&quot;</span><span class="punctuation">,</span></span><br><span class="line">            <span class="attr">&quot;size&quot;</span><span class="punctuation">:</span><span class="number">10</span>         size表示分类的情况</span><br><span class="line">        <span class="punctuation">&#125;</span></span><br><span class="line">    <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;ageAvg&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span>                自定义聚合名</span><br><span class="line">        <span class="attr">&quot;avg&quot;</span><span class="punctuation">:</span><span class="punctuation">&#123;</span>               avg表示求指定字段的平均值</span><br><span class="line">            <span class="attr">&quot;field&quot;</span><span class="punctuation">:</span><span class="string">&quot;age&quot;</span></span><br><span class="line">        <span class="punctuation">&#125;</span></span><br><span class="line">    <span class="punctuation">&#125;</span></span><br><span class="line">    <span class="punctuation">&#125;</span><span class="punctuation">,</span></span><br><span class="line">    <span class="attr">&quot;size&quot;</span><span class="punctuation">:</span><span class="number">0</span>                  size表示只显示聚合内容，不显示hits内容</span><br><span class="line"><span class="punctuation">&#125;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">按照所有人年龄聚合，并且请求这些年龄段的这些人的平均薪资</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;query&quot;:&#123;</span><br><span class="line">        &quot;match_all&quot;:&#123;&#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;aggs&quot;:&#123;</span><br><span class="line">        &quot;ageAvg&quot;:&#123;</span><br><span class="line">            &quot;terms&quot;:&#123;</span><br><span class="line">                &quot;field&quot;:&quot;age&quot;,</span><br><span class="line">                &quot;size&quot;:<span class="number">100</span></span><br><span class="line">            &#125;,</span><br><span class="line">             &quot;aggs&quot;:&#123;</span><br><span class="line">                    &quot;abc&quot;:&#123;</span><br><span class="line">                        &quot;avg&quot;:&#123;</span><br><span class="line">                        &quot;field&quot;:&quot;age&quot;</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;size&quot;:<span class="number">0</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">响应：</span><br><span class="line">&quot;buckets&quot;: [</span><br><span class="line">                &#123;</span><br><span class="line">                    &quot;key&quot;: <span class="number">31</span>,            年龄为<span class="number">31</span>的有<span class="number">61</span>个</span><br><span class="line">                    &quot;doc_count&quot;: <span class="number">61</span>,</span><br><span class="line">                    &quot;abc&quot;: &#123;</span><br><span class="line">                        &quot;value&quot;: <span class="number">28312.918032786885</span></span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &#123;</span><br><span class="line">                    &quot;key&quot;: <span class="number">39</span>,            年龄为<span class="number">39</span>的有<span class="number">60</span>个</span><br><span class="line">                    &quot;doc_count&quot;: <span class="number">60</span>,</span><br><span class="line">                    &quot;abc&quot;: &#123;</span><br><span class="line">                        &quot;value&quot;: <span class="number">25269.583333333332</span></span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">   				 ......</span><br></pre></td></tr></table></figure>

<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">查出所有年龄分布，并且这些年龄段中性别为 M 的平均薪资和 F 的平均薪资以及这个年龄段的总体平均薪资</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;query&quot;:&#123;</span><br><span class="line">        &quot;match_all&quot;:&#123;&#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;aggs&quot;:&#123;</span><br><span class="line">        &quot;ageAvg&quot;:&#123;</span><br><span class="line">            &quot;terms&quot;:&#123;</span><br><span class="line">                &quot;field&quot;:&quot;age&quot;,</span><br><span class="line">                &quot;size&quot;:<span class="number">100</span></span><br><span class="line">            &#125;,</span><br><span class="line">            &quot;aggs&quot;:&#123;</span><br><span class="line">                &quot;genderAgg&quot;:&#123;</span><br><span class="line">                    &quot;terms&quot;:&#123;</span><br><span class="line">                        &quot;field&quot;:&quot;gender.keyword&quot;,    求每个年龄段的不同性别的平均薪资</span><br><span class="line">                        &quot;size&quot;:<span class="number">10</span></span><br><span class="line">                    &#125;,</span><br><span class="line">                    &quot;aggs&quot;:&#123;</span><br><span class="line">                        &quot;genderAgg&quot;:&#123;</span><br><span class="line">                            &quot;avg&quot;:&#123;</span><br><span class="line">                                &quot;field&quot;:&quot;balance&quot;</span><br><span class="line">                            &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125; ,</span><br><span class="line">                        &quot;ageBalanceAvg&quot;:&#123;                整个年龄段的平均薪资</span><br><span class="line">                            &quot;avg&quot;:&#123;</span><br><span class="line">                                &quot;field&quot;:&quot;balance&quot;</span><br><span class="line">                            &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;size&quot;:<span class="number">0</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="9-Mapping"><a href="#9-Mapping" class="headerlink" title="9. Mapping"></a>9. Mapping</h3><p>用来定义一个文档，以及所包含的属性是如何存储和索引的</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">GET</span> http:<span class="operator">/</span><span class="operator">/</span><span class="number">192.168</span><span class="number">.253</span><span class="number">.128</span>:<span class="number">9200</span><span class="operator">/</span>bank<span class="operator">/</span>_mapping</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">    &quot;bank&quot;: &#123;</span><br><span class="line">        &quot;mappings&quot;: &#123;</span><br><span class="line">            &quot;properties&quot;: &#123;</span><br><span class="line">                &quot;account_number&quot;: &#123;</span><br><span class="line">                    &quot;type&quot;: &quot;long&quot;            自动确认字段的属性</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;address&quot;: &#123;</span><br><span class="line">                    &quot;type&quot;: &quot;text&quot;,</span><br><span class="line">                    &quot;fields&quot;: &#123;</span><br><span class="line">                        &quot;keyword&quot;: &#123;</span><br><span class="line">                            &quot;type&quot;: &quot;keyword&quot;,        默认给每个text属性添加了一个keyword属性</span><br><span class="line">                            &quot;ignore_above&quot;: <span class="number">256</span></span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;age&quot;: &#123;</span><br><span class="line">                    &quot;type&quot;: &quot;long&quot;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;balance&quot;: &#123;</span><br><span class="line">                    &quot;type&quot;: &quot;long&quot;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;city&quot;: &#123;</span><br><span class="line">                    &quot;type&quot;: &quot;text&quot;,</span><br><span class="line">                    &quot;fields&quot;: &#123;</span><br><span class="line">                        &quot;keyword&quot;: &#123;</span><br><span class="line">                            &quot;type&quot;: &quot;keyword&quot;,</span><br><span class="line">                            &quot;ignore_above&quot;: <span class="number">256</span></span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;,</span><br></pre></td></tr></table></figure>

<p>指定每个字段映射，<strong>指定字段属性</strong></p>
<h4 id="1-创建索引映射"><a href="#1-创建索引映射" class="headerlink" title="1. 创建索引映射"></a>1. 创建索引映射</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">PUT http:<span class="operator">/</span><span class="operator">/</span><span class="number">192.168</span><span class="number">.253</span><span class="number">.128</span>:<span class="number">9200</span><span class="operator">/</span>my_index  添加一个新的索引，并指定每个字段的属性</span><br><span class="line">&#123;</span><br><span class="line">    &quot;mapping&quot;:&#123;</span><br><span class="line">        &quot;properties&quot;:&#123;</span><br><span class="line">            &quot;age&quot;:&#123;&quot;type&quot;:&quot;integer&quot;&#125;,</span><br><span class="line">            &quot;email&quot;:&#123;&quot;type&quot;:&quot;keyword&quot;&#125;,    属性为keyword表示只能是精确匹配</span><br><span class="line">            &quot;name&quot;:&#123;&quot;type&quot;:&quot;text&quot;&#125;         属性为text表示全文匹配</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="2-修改索引映射–添加一个字段"><a href="#2-修改索引映射–添加一个字段" class="headerlink" title="2. 修改索引映射–添加一个字段"></a>2. 修改索引映射–添加一个字段</h4><p>_mapping这个是必须的</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">PUT http:<span class="operator">/</span><span class="operator">/</span><span class="number">192.168</span><span class="number">.253</span><span class="number">.128</span>:<span class="number">9200</span><span class="operator">/</span>my_index<span class="operator">/</span>_mapping</span><br><span class="line">&#123;</span><br><span class="line">        &quot;properties&quot;:&#123;</span><br><span class="line">            &quot;employee-id&quot;:&#123;</span><br><span class="line">                &quot;type&quot;:&quot;keyword&quot;,</span><br><span class="line">                &quot;index&quot;:<span class="literal">false</span>      表示该字段不会被索引</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="3-更新映射"><a href="#3-更新映射" class="headerlink" title="3. 更新映射"></a>3. 更新映射</h4><p>不能更新映射，<strong>可以重新创建新的索引，进行数据迁移</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">PUT http:<span class="operator">/</span><span class="operator">/</span><span class="number">192.168</span><span class="number">.253</span><span class="number">.128</span>:<span class="number">9200</span><span class="operator">/</span>newbank</span><br><span class="line">&#123;</span><br><span class="line">    &quot;mappings&quot;:&#123;</span><br><span class="line">        &quot;properties&quot;:&#123;</span><br><span class="line">            &quot;account_number&quot;: &#123;</span><br><span class="line">                    &quot;type&quot;: &quot;long&quot;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;address&quot;: &#123;</span><br><span class="line">                    &quot;type&quot;: &quot;text&quot;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;age&quot;: &#123;</span><br><span class="line">                    &quot;type&quot;: &quot;long&quot;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;balance&quot;: &#123;</span><br><span class="line">                    &quot;type&quot;: &quot;long&quot;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;city&quot;: &#123;</span><br><span class="line">                    &quot;type&quot;: &quot;text&quot;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;email&quot;: &#123;</span><br><span class="line">                    &quot;type&quot;: &quot;text&quot;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;employer&quot;: &#123;</span><br><span class="line">                    &quot;type&quot;: &quot;text&quot;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;firstname&quot;: &#123;</span><br><span class="line">                    &quot;type&quot;: &quot;text&quot;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;gender&quot;: &#123;</span><br><span class="line">                    &quot;type&quot;: &quot;text&quot;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;lastname&quot;: &#123;</span><br><span class="line">                    &quot;type&quot;: &quot;text&quot;</span><br><span class="line">                &#125;,</span><br><span class="line">                &quot;state&quot;: &#123;</span><br><span class="line">                    &quot;type&quot;: &quot;text&quot;</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="4-数据迁移"><a href="#4-数据迁移" class="headerlink" title="4. 数据迁移"></a>4. 数据迁移</h4><p>先创建出新的正确映射</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">6.0</span>之前的版本，映射带有类型，采用方式一</span><br><span class="line">&#123;</span><br><span class="line">    &quot;source&quot;:&#123;</span><br><span class="line">        &quot;index&quot;:&quot;bank&quot;,</span><br><span class="line">        &quot;type&quot;:&quot;account&quot;</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;dest&quot;:&#123;</span><br><span class="line">        &quot;index&quot;:&quot;newbank&quot;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="number">7.0</span>之后的版本，没有类型了，采用方式二</span><br><span class="line">&#123;</span><br><span class="line">    &quot;source&quot;:&#123;</span><br><span class="line">        &quot;index&quot;:&quot;bank&quot;</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;dest&quot;:&#123;</span><br><span class="line">        &quot;index&quot;:&quot;newbank&quot;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="10-分词"><a href="#10-分词" class="headerlink" title="10. 分词"></a>10. 分词</h3><p>把一段话划分为一个个单词（token）</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line">POST http:<span class="operator">/</span><span class="operator">/</span><span class="number">192.168</span><span class="number">.253</span><span class="number">.128</span>:<span class="number">9200</span><span class="operator">/</span>_analyze</span><br><span class="line">&#123;</span><br><span class="line">    &quot;analyzer&quot;:&quot;standard&quot;,              标准分词器</span><br><span class="line">    &quot;text&quot;:&quot;There are a lot of pigs&quot;</span><br><span class="line">&#125;</span><br><span class="line">响应，把文本分为了一个个单词</span><br><span class="line">&#123;</span><br><span class="line">    &quot;tokens&quot;: [</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;token&quot;: &quot;there&quot;,</span><br><span class="line">            &quot;start_offset&quot;: <span class="number">0</span>,</span><br><span class="line">            &quot;end_offset&quot;: <span class="number">5</span>,</span><br><span class="line">            &quot;type&quot;: &quot;&lt;ALPHANUM&gt;&quot;,</span><br><span class="line">            &quot;position&quot;: <span class="number">0</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;token&quot;: &quot;are&quot;,</span><br><span class="line">            &quot;start_offset&quot;: <span class="number">6</span>,</span><br><span class="line">            &quot;end_offset&quot;: <span class="number">9</span>,</span><br><span class="line">            &quot;type&quot;: &quot;&lt;ALPHANUM&gt;&quot;,</span><br><span class="line">            &quot;position&quot;: <span class="number">1</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;token&quot;: &quot;a&quot;,</span><br><span class="line">            &quot;start_offset&quot;: <span class="number">10</span>,</span><br><span class="line">            &quot;end_offset&quot;: <span class="number">11</span>,</span><br><span class="line">            &quot;type&quot;: &quot;&lt;ALPHANUM&gt;&quot;,</span><br><span class="line">            &quot;position&quot;: <span class="number">2</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;token&quot;: &quot;lot&quot;,</span><br><span class="line">            &quot;start_offset&quot;: <span class="number">12</span>,</span><br><span class="line">            &quot;end_offset&quot;: <span class="number">15</span>,</span><br><span class="line">            &quot;type&quot;: &quot;&lt;ALPHANUM&gt;&quot;,</span><br><span class="line">            &quot;position&quot;: <span class="number">3</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;token&quot;: &quot;of&quot;,</span><br><span class="line">            &quot;start_offset&quot;: <span class="number">16</span>,</span><br><span class="line">            &quot;end_offset&quot;: <span class="number">18</span>,</span><br><span class="line">            &quot;type&quot;: &quot;&lt;ALPHANUM&gt;&quot;,</span><br><span class="line">            &quot;position&quot;: <span class="number">4</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            &quot;token&quot;: &quot;pigs&quot;,</span><br><span class="line">            &quot;start_offset&quot;: <span class="number">19</span>,</span><br><span class="line">            &quot;end_offset&quot;: <span class="number">23</span>,</span><br><span class="line">            &quot;type&quot;: &quot;&lt;ALPHANUM&gt;&quot;,</span><br><span class="line">            &quot;position&quot;: <span class="number">5</span></span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong>中文分词器ik</strong></p>
<p>自定义词库</p>
<h3 id="11-安装nginx"><a href="#11-安装nginx" class="headerlink" title="11. 安装nginx"></a>11. 安装nginx</h3><h2 id="五，整合Springboot"><a href="#五，整合Springboot" class="headerlink" title="五，整合Springboot"></a>五，整合Springboot</h2><h3 id="1-入门"><a href="#1-入门" class="headerlink" title="1. 入门"></a>1. 入门</h3><p>安装依赖</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&lt;dependency&gt;</span></span><br><span class="line">            <span class="string">&lt;groupId&gt;org.elasticsearch.client&lt;/groupId&gt;</span></span><br><span class="line">            <span class="string">&lt;artifactId&gt;elasticsearch-rest-high-level-client&lt;/artifactId&gt;</span></span><br><span class="line">            <span class="string">&lt;version&gt;7.4.2&lt;/version&gt;</span></span><br><span class="line">        <span class="string">&lt;/dependency&gt;</span></span><br></pre></td></tr></table></figure>

<p>新建一个配置类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">GulimallElasticSearchConfig</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> RequestOptions COMMON_OPTIONS;</span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">        RequestOptions.<span class="type">Builder</span> <span class="variable">builder</span> <span class="operator">=</span> RequestOptions.DEFAULT.toBuilder();</span><br><span class="line">        COMMON_OPTIONS = builder.build();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="keyword">public</span> RestHighLevelClient <span class="title function_">esRestClient</span><span class="params">()</span>&#123;</span><br><span class="line">        <span class="type">RestHighLevelClient</span> <span class="variable">client</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">RestHighLevelClient</span>(</span><br><span class="line">                RestClient.builder(</span><br><span class="line">                        <span class="keyword">new</span> <span class="title class_">HttpHost</span>(<span class="string">&quot;192.168.253.128&quot;</span>, <span class="number">9200</span>, <span class="string">&quot;http&quot;</span>)</span><br><span class="line">                )</span><br><span class="line">        );</span><br><span class="line">        <span class="keyword">return</span> client;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>测试</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line">   <span class="keyword">void</span> <span class="title function_">indexData</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">       <span class="type">IndexRequest</span> <span class="variable">indexRequest</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">IndexRequest</span>(<span class="string">&quot;users&quot;</span>);</span><br><span class="line">       indexRequest.id(<span class="string">&quot;1&quot;</span>); <span class="comment">//数据的id</span></span><br><span class="line">       <span class="type">User</span> <span class="variable">user</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">User</span>(<span class="string">&quot;黄红提&quot;</span>,<span class="string">&quot;男&quot;</span>);</span><br><span class="line">       <span class="type">String</span> <span class="variable">json</span> <span class="operator">=</span> JSON.toJSONString(user);</span><br><span class="line">       indexRequest.source(json, XContentType.JSON); <span class="comment">// 要保存的内容</span></span><br><span class="line"></span><br><span class="line">       <span class="comment">// 执行操作</span></span><br><span class="line">       <span class="type">IndexResponse</span> <span class="variable">index</span> <span class="operator">=</span> restHighLevelClient.index(indexRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);</span><br><span class="line">       System.out.println(index);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-复杂的检索操作"><a href="#2-复杂的检索操作" class="headerlink" title="2. 复杂的检索操作"></a>2. 复杂的检索操作</h3><p><strong>查询到的数据</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    &quot;took&quot;: <span class="number">4</span>,</span><br><span class="line">    &quot;timed_out&quot;: <span class="literal">false</span>,</span><br><span class="line">    &quot;_shards&quot;: &#123;</span><br><span class="line">        &quot;total&quot;: <span class="number">1</span>,</span><br><span class="line">        &quot;successful&quot;: <span class="number">1</span>,</span><br><span class="line">        &quot;skipped&quot;: <span class="number">0</span>,</span><br><span class="line">        &quot;failed&quot;: <span class="number">0</span></span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;hits&quot;: &#123;</span><br><span class="line">        &quot;total&quot;: &#123;</span><br><span class="line">            &quot;value&quot;: <span class="number">1000</span>,</span><br><span class="line">            &quot;relation&quot;: &quot;eq&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;max_score&quot;: <span class="keyword">null</span>,</span><br><span class="line">        &quot;hits&quot;: []</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;aggregations&quot;: &#123;</span><br><span class="line">        &quot;ageAvg&quot;: &#123;</span><br><span class="line">            &quot;doc_count_error_upper_bound&quot;: <span class="number">0</span>,</span><br><span class="line">            &quot;sum_other_doc_count&quot;: <span class="number">0</span>,</span><br><span class="line">            &quot;buckets&quot;: [</span><br><span class="line">                &#123;</span><br><span class="line">                    &quot;key&quot;: <span class="number">31</span>,</span><br><span class="line">                    &quot;doc_count&quot;: <span class="number">61</span>,</span><br><span class="line">                    &quot;avgAge&quot;: &#123;</span><br><span class="line">                        &quot;value&quot;: <span class="number">31.0</span></span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &#123;</span><br><span class="line">                    &quot;key&quot;: <span class="number">39</span>,</span><br><span class="line">                    &quot;doc_count&quot;: <span class="number">60</span>,</span><br><span class="line">                    &quot;avgAge&quot;: &#123;</span><br><span class="line">                        &quot;value&quot;: <span class="number">39.0</span></span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;,</span><br></pre></td></tr></table></figure>

<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"> <span class="variable">@Test</span></span><br><span class="line">    void indexData() throws IOException &#123;</span><br><span class="line">        <span class="operator">/</span><span class="operator">/</span> 创建检索请求</span><br><span class="line">        SearchRequest searchRequest <span class="operator">=</span> <span class="keyword">new</span> SearchRequest();</span><br><span class="line">        <span class="operator">/</span><span class="operator">/</span>检索的索引</span><br><span class="line">        searchRequest.indices(&quot;bank&quot;);</span><br><span class="line">        <span class="operator">/</span><span class="operator">/</span>指定检索条件</span><br><span class="line">        SearchSourceBuilder sourceBuilder <span class="operator">=</span> <span class="keyword">new</span> SearchSourceBuilder();</span><br><span class="line">        <span class="operator">/</span><span class="operator">/</span>构造检索条件</span><br><span class="line"><span class="operator">/</span><span class="operator">/</span>        sourceBuilder.from();</span><br><span class="line"><span class="operator">/</span><span class="operator">/</span>        sourceBuilder.size();</span><br><span class="line"><span class="operator">/</span><span class="operator">/</span>        sourceBuilder.aggregation();</span><br><span class="line">        <span class="operator">/</span><span class="operator">/</span> 查询条件</span><br><span class="line">        sourceBuilder.query(QueryBuilders.matchQuery(&quot;address&quot;,&quot;mill&quot;));</span><br><span class="line">        <span class="operator">/</span><span class="operator">/</span> 按照年龄聚合条件</span><br><span class="line">        TermsAggregationBuilder ageAgg <span class="operator">=</span> AggregationBuilders.terms(&quot;ageAgg&quot;).field(&quot;age&quot;).size(<span class="number">10</span>);</span><br><span class="line">        sourceBuilder.aggregation(ageAgg);</span><br><span class="line">        <span class="operator">/</span><span class="operator">/</span> 计算平均薪资</span><br><span class="line">        AvgAggregationBuilder balanceAvg <span class="operator">=</span> AggregationBuilders.<span class="built_in">avg</span>(&quot;balanceAvg&quot;).field(&quot;balance&quot;);</span><br><span class="line">        sourceBuilder.aggregation(balanceAvg);</span><br><span class="line"></span><br><span class="line">        searchRequest.source(sourceBuilder);</span><br><span class="line">        <span class="operator">/</span><span class="operator">/</span>执行检索</span><br><span class="line">        SearchResponse searchResponse <span class="operator">=</span> client.search(searchRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);</span><br><span class="line"></span><br><span class="line">        <span class="operator">/</span><span class="operator">/</span>分析结果searchResponse</span><br><span class="line">        System.out.println(searchResponse.toString());</span><br><span class="line"></span><br><span class="line">        <span class="operator">/</span><span class="operator">/</span>获取所有查到的数据</span><br><span class="line">        SearchHits hits <span class="operator">=</span> searchResponse.getHits();</span><br><span class="line">        <span class="operator">/</span><span class="operator">/</span>真正命中的记录</span><br><span class="line">        SearchHit[] hits1 <span class="operator">=</span> hits.getHits();</span><br><span class="line">        <span class="keyword">for</span> (SearchHit hit: hits1) &#123;</span><br><span class="line">            String string <span class="operator">=</span> hit.getSourceAsString();</span><br><span class="line">            <span class="operator">/</span><span class="operator">/</span> 将返回结果映射到具体类上</span><br><span class="line">            Account account <span class="operator">=</span> JSON.parseObject(string, Account.class);</span><br><span class="line">            System.out.println(account.toString());</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="operator">/</span><span class="operator">/</span> 获取到分析信息（如聚合操作获取到的信息）</span><br><span class="line">        Aggregations aggregations <span class="operator">=</span> searchResponse.getAggregations();</span><br><span class="line">        <span class="operator">/</span><span class="operator">/</span> Terms是根据前面AggregationBuilders.terms</span><br><span class="line">        Terms ageAgg1 <span class="operator">=</span> aggregations.get(&quot;ageAgg&quot;);</span><br><span class="line">        <span class="keyword">for</span> (Terms.Bucket bucket:ageAgg1.getBuckets())&#123;</span><br><span class="line">            String keyAsString <span class="operator">=</span> bucket.getKeyAsString();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="operator">/</span><span class="operator">/</span> 求平均薪资，类型为Avg</span><br><span class="line">        Avg balanceAvg1 <span class="operator">=</span> aggregations.get(&quot;balanceAvg&quot;);</span><br><span class="line">        System.out.println(&quot;平均薪资&quot;<span class="operator">+</span>balanceAvg1.getValue());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h1 id="六、ES框架的Easy-ES快速开发"><a href="#六、ES框架的Easy-ES快速开发" class="headerlink" title="六、ES框架的Easy-ES快速开发"></a>六、ES框架的Easy-ES快速开发</h1>
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/ElasticSearch/" rel="tag"># ElasticSearch</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2023/10/24/index/" rel="prev" title="我的博客">
                我的博客 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.png"
                alt="" />
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#ElasticSearch-%E4%BA%86%E8%A7%A3"><span class="nav-number">1.</span> <span class="nav-text">ElasticSearch(了解)</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%80-%E3%80%81%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="nav-number">1.1.</span> <span class="nav-text">一 、基本概念</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8C-%E3%80%81Docker%E5%AE%89%E8%A3%85"><span class="nav-number">1.2.</span> <span class="nav-text">二 、Docker安装</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Kibanna%E5%8F%AF%E8%A7%86%E5%8C%96%E7%95%8C%E9%9D%A2"><span class="nav-number">1.2.1.</span> <span class="nav-text">Kibanna可视化界面</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E5%88%9D%E6%AD%A5%E6%8E%A2%E7%B4%A2"><span class="nav-number">1.3.</span> <span class="nav-text">三、初步探索</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-cat"><span class="nav-number">1.3.1.</span> <span class="nav-text">1. _cat</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E7%B4%A2%E5%BC%95%E4%B8%80%E4%B8%AA%E6%96%87%E6%A1%A3%EF%BC%88%E4%BF%9D%E5%AD%98%E5%92%8C%E4%BF%AE%E6%94%B9%EF%BC%89"><span class="nav-number">1.3.2.</span> <span class="nav-text">2. 索引一个文档（保存和修改）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-POST%E5%92%8CPUT%E5%8C%BA%E5%88%AB"><span class="nav-number">1.3.3.</span> <span class="nav-text">3. POST和PUT区别</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E6%9F%A5%E8%AF%A2%E6%96%87%E6%A1%A3%EF%BC%88query%EF%BC%89"><span class="nav-number">1.3.4.</span> <span class="nav-text">3. 查询文档（query）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-%E6%9B%B4%E6%96%B0%E6%96%87%E6%A1%A3%EF%BC%88update%EF%BC%89"><span class="nav-number">1.3.5.</span> <span class="nav-text">4. 更新文档（update）</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-POST%E6%9B%B4%E6%96%B0%E2%80%94-update%E2%80%94%E2%80%94%E5%BF%85%E9%A1%BB%E5%B8%A6-%E2%80%9Cdoc%E2%80%9D"><span class="nav-number">1.3.5.1.</span> <span class="nav-text">1. POST更新—-_update——必须带 “doc”</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-POST%E6%9B%B4%E6%96%B0%E2%80%94-%E4%B8%8D%E5%B8%A6-update%E2%80%94-%E4%B8%8D%E5%B8%A6%E2%80%9Cdoc%E2%80%9D"><span class="nav-number">1.3.5.2.</span> <span class="nav-text">2. POST更新—-不带_update—-不带“doc”</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-PUT%E6%9B%B4%E6%96%B0%E5%92%8C2%E7%9B%B8%E5%90%8C"><span class="nav-number">1.3.5.3.</span> <span class="nav-text">3. PUT更新和2相同</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-POST%E2%80%94%E5%B8%A6-update%E6%9B%B4%E6%96%B0%E5%90%8C%E6%97%B6%E5%A2%9E%E5%8A%A0%E5%B1%9E%E6%80%A7"><span class="nav-number">1.3.5.4.</span> <span class="nav-text">4. POST—带_update更新同时增加属性</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-PUT-%E6%9B%B4%E6%96%B0%E5%90%8C%E6%97%B6%E5%A2%9E%E5%8A%A0%E5%B1%9E%E6%80%A7"><span class="nav-number">1.3.5.5.</span> <span class="nav-text">5. PUT 更新同时增加属性</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-%E5%88%A0%E9%99%A4%E6%96%87%E6%A1%A3-%E7%B4%A2%E5%BC%95"><span class="nav-number">1.3.6.</span> <span class="nav-text">5. 删除文档&amp;索引</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-bulk%E6%89%B9%E9%87%8FAPI"><span class="nav-number">1.3.7.</span> <span class="nav-text">6. bulk批量API</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E8%BF%9B%E9%98%B6%E6%A3%80%E7%B4%A2"><span class="nav-number">1.4.</span> <span class="nav-text">四、进阶检索</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-SearchAPI"><span class="nav-number">1.4.1.</span> <span class="nav-text">1. SearchAPI</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-match%E5%8C%B9%E9%85%8D%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.4.2.</span> <span class="nav-text">2. match匹配查询</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-match-phrase%E7%9F%AD%E8%AF%AD%E5%8C%B9%E9%85%8D"><span class="nav-number">1.4.3.</span> <span class="nav-text">3. match_phrase短语匹配</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-multi-match%E5%A4%9A%E5%AD%97%E6%AE%B5%E5%8C%B9%E9%85%8D"><span class="nav-number">1.4.4.</span> <span class="nav-text">4. multi_match多字段匹配</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-bool%E5%A4%8D%E5%90%88%E6%9F%A5%E8%AF%A2"><span class="nav-number">1.4.5.</span> <span class="nav-text">5. bool复合查询</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-%E7%BB%93%E6%9E%9C%E8%BF%87%E6%BB%A4filter"><span class="nav-number">1.4.6.</span> <span class="nav-text">6. 结果过滤filter</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-term"><span class="nav-number">1.4.7.</span> <span class="nav-text">7. term</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#8-aggregations%E6%89%A7%E8%A1%8C%E8%81%9A%E5%90%88"><span class="nav-number">1.4.8.</span> <span class="nav-text">8. aggregations执行聚合</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-Mapping"><span class="nav-number">1.4.9.</span> <span class="nav-text">9. Mapping</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-%E5%88%9B%E5%BB%BA%E7%B4%A2%E5%BC%95%E6%98%A0%E5%B0%84"><span class="nav-number">1.4.9.1.</span> <span class="nav-text">1. 创建索引映射</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-%E4%BF%AE%E6%94%B9%E7%B4%A2%E5%BC%95%E6%98%A0%E5%B0%84%E2%80%93%E6%B7%BB%E5%8A%A0%E4%B8%80%E4%B8%AA%E5%AD%97%E6%AE%B5"><span class="nav-number">1.4.9.2.</span> <span class="nav-text">2. 修改索引映射–添加一个字段</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-%E6%9B%B4%E6%96%B0%E6%98%A0%E5%B0%84"><span class="nav-number">1.4.9.3.</span> <span class="nav-text">3. 更新映射</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-%E6%95%B0%E6%8D%AE%E8%BF%81%E7%A7%BB"><span class="nav-number">1.4.9.4.</span> <span class="nav-text">4. 数据迁移</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#10-%E5%88%86%E8%AF%8D"><span class="nav-number">1.4.10.</span> <span class="nav-text">10. 分词</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#11-%E5%AE%89%E8%A3%85nginx"><span class="nav-number">1.4.11.</span> <span class="nav-text">11. 安装nginx</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%94%EF%BC%8C%E6%95%B4%E5%90%88Springboot"><span class="nav-number">1.5.</span> <span class="nav-text">五，整合Springboot</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E5%85%A5%E9%97%A8"><span class="nav-number">1.5.1.</span> <span class="nav-text">1. 入门</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E5%A4%8D%E6%9D%82%E7%9A%84%E6%A3%80%E7%B4%A2%E6%93%8D%E4%BD%9C"><span class="nav-number">1.5.2.</span> <span class="nav-text">2. 复杂的检索操作</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%AD%E3%80%81ES%E6%A1%86%E6%9E%B6%E7%9A%84Easy-ES%E5%BF%AB%E9%80%9F%E5%BC%80%E5%8F%91"><span class="nav-number">2.</span> <span class="nav-text">六、ES框架的Easy-ES快速开发</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">黄宏涛</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
